package java01;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.util.Comparator;
import java.util.Map;

//Cloneable接口只是作为一个标记，它唯一的作用是允许在类型查询中使用instanceof。但是如果重写clone方法但是没有实现该接口，就会生成一个受查异常
public class Person extends Parent implements Comparator<Person>,Comparable<Person>,Cloneable, ActionListener {
    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person(String name) {
        this.name = name;
    }

    public Person(Map<String, Object> map) {
        this.name= (String) map.get("name");
        this.age= (int)map.get("age");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(Person p) {
        return Integer.compare(age,p.getAge());
    }

    @Override
    public int compare(Person o1, Person o2) {
        return o1.getName().length()-o2.getName().length();
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按频率打印到控制台"+ LocalDate.now());
        Toolkit.getDefaultToolkit().beep();//发出响铃
    }

    //Object默认的clone()是protected修饰的
    //如果声明成public子类使用父类重写的clone方法那么子类的对象域就不会深克隆，protected修饰是为了让其重写父类的方法
    @Override
    public Person clone() throws CloneNotSupportedException {
        return (Person) super.clone();
    }
}
